<template>
    <div class="">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-message"></i> 分站账号列表</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <el-button type="primary" round @click="showAdd(0,0)"><i class="el-icon-plus"> </i>&nbsp;&nbsp;新增分站账号</el-button>
            <div style="height:40px"></div>
                <el-form :inline="true"  class="demo-form-inline">
                    <el-form-item label="账号名称:">
                        <el-input v-model="name" placeholder="请输入账号名称" clearable style="width:300px;"></el-input>
                    </el-form-item>
                    <el-form-item label="手机号码:">
                        <el-input v-model="mobile" placeholder="请输入手机号码" clearable style="width:300px;"></el-input>
                    </el-form-item>
                    <el-form-item label="昵称:">
                        <el-input v-model="nickName" placeholder="请输入昵称" clearable style="width:300px;"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="onSelect"> &nbsp;&nbsp;查询&nbsp;&nbsp;  </el-button>
                        <el-button type="primary" @click="onResetting"> &nbsp;&nbsp;重置&nbsp;&nbsp;  </el-button>
                    </el-form-item>
                </el-form>
                <el-table :data="tableData"  style="width: 100%"   ref="multipleTable">
                    <el-table-column prop="account_id" label="账号ID" width="100" align="center">
                    </el-table-column>
                    <el-table-column prop="city_name" label="所属分站" width="100" align="center">
                    </el-table-column>
                    <el-table-column prop="user_name" label="账号名称" width="200" align="center">
                    </el-table-column>
                     <el-table-column prop="nick_name" label="昵称" width="200" align="center">
                    </el-table-column>
                     <el-table-column prop="mobile" label="手机号码" width="200" align="center">
                    </el-table-column>
                      <el-table-column prop="create_time" label="创建时间" width="300" align="center">
                    </el-table-column>
                    <el-table-column label="操作" align="center" >
                        <template slot-scope="scope" >
                            <el-button class="edit" size="small" type="text" @click="showAdd(scope.$index, scope.row)">编辑</el-button>
                            <el-button class="del" size="small" type="text"  @click="handleDelete(scope.$index, scope.row)" >删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="pagination" v-show="totalPages>1">
                    <el-pagination @current-change="handleCurrentChange" layout="prev, pager, next" :page-size="pageSize" :total="totalElements" background>
                    </el-pagination>
                </div>
                
        </div>
        <!-- 新增编辑弹出框 -->
        <el-dialog :title="title" :visible.sync="isAddAccout"  center >
            <el-form style="width:460px; margin:0 auto">
                <el-form-item label="账号名称">
                     <el-input style="width:300px;" v-model="eName" auto-complete="off"  placeholder="请输入账号名称" ></el-input>
                </el-form-item>
                <el-form-item label="所属分站">
                <el-select v-model="id" placeholder="请选择所属分站" style="width:300px;">
                     <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id">
                     </el-option>
                </el-select>
                </el-form-item>
                <el-form-item label="账号密码">
                     <el-input style="width:300px;" v-model="ePwd" auto-complete="off"  placeholder="请输入账号密码" ></el-input>
                </el-form-item>
                 <el-form-item label="账号昵称">
                     <el-input style="width:300px;" v-model="eNickName" auto-complete="off"  placeholder="请输入账号昵称" ></el-input>
                </el-form-item> 
                <el-form-item label="手机号码">
                     <el-input style="width:300px;" v-model="eMobile" auto-complete="off"  placeholder="请输入手机号码" ></el-input>
                </el-form-item> 
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="onCancel">取 消</el-button>
                <el-button type="primary" @click="onCommit">确 定</el-button>
            </div>
        </el-dialog>
        <!-- 删除提示框 -->
        <el-dialog title="提示" :visible.sync="delVisible" width="300px" center>
            <div class="del-dialog-cnt">删除不可恢复，是否确定删除？</div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="delVisible = false">取 消</el-button>
                <el-button type="primary" @click="deleteRow">确 定</el-button>
            </span>
        </el-dialog>
        <!-- / 删除提示框 -->
    </div>
</template>
<script>
import { 
    getSubstationAccountList,postDeleteSubstationAccount,
    postAddSubstationAccount,postEditSubstationAccount,getSubstationListAll } from '@/api/substation'
export default {
    name:"substationList",
    data() {
        return {
            pageIndex:1,//当前页码
            pageSize:10,//每页10条
            totalElements:1,//默认的总条数
            totalPages:1,//默认的总页数
            tableData:[],//表格数据
            cityId:0,//默认0代表全部
            delIndex:0, //当前选中的下标
            delId:0,  //当前要删除的Id
            delVisible:false, //默认dialog 不弹出
            name:'', //账户名
            mobile:'',//手机号
            nickName:'',//昵称
            isAddAccout:false,//是否弹出新增分站账号界面
            eName:'',//编辑或新增的名称，编辑有数据 新增无
            ePwd:'',//编辑或新增密码
            eNickName:'',//编辑或新增昵称
            eMobile:'',//编辑或新增手机
            title:'新增分站账号信息',//弹出框标题
            options:[],//分站下来选项
            accountId:'',//当前账户id
            id:'',//下拉选项id
            accountData:{},//通过id查询到的账号信息
            dialogState:false,//弹出框的状态 false代表新增 true是修改
        }
    },
    //实例创建
    created(){
        //获得表格数据
        this.getDate();
        //获得分站列表
        this.getSubstationList();
    },
    //计算属性
    computed:{

    },
    //方法
    methods: {
        //获取数据
        getDate(){
            getSubstationAccountList(this.cityId,this.name,this.mobile,this.nickName,this.pageIndex,this.pageSize).then((response)=> {
                // console.log(response);
                if(response.code==200){
                    var data = response.data;//当前返回的数据
                    this.tableData = data.list;//列表数据
                    this.totalElements = data.total; //总条数
                    this.totalPages = Math.ceil(data.total/this.pageSize);//总页数
                }else{
                    this.$notify.error({
                        title: '异常',
                        offset: 100,
                        message: response.msg
                    });
                }
            })
        },
        //获得分站列表
        getSubstationList(){
            getSubstationListAll().then((response)=> {
                // console.log(response);
                if(response.code==200){
                    this.options = response.data;//option
                }else{
                    this.$notify.error({
                        title: '异常',
                        offset: 100,
                        message: response.msg
                    });
                }
            })
        },
        //显示增加/编辑账号弹出框
        showAdd(index,row){
            // console.log(index,row.account_id);
            if(row.account_id && row.account_id>0){//编辑状态
                this.accountId = row.account_id;//获得当前账户id
                this.dialogState = true;//改变状态
                this.title = "编辑分站账号信息";
                getSubstationAccountList(0,'','','',1,999999).then((response)=> {//获得所有账号信息
                if(response.code==200){
                    var data = response.data.list;//当前返回的数据
                    //遍历查找与当前account_id相同的
                    for(let i = 0; i<data.length;i++){
                        if(data[i].account_id == row.account_id){//找到了
                            this.accountData = data[i];
                            console.log(this.accountData);
                            //数据赋值
                            this.eName = this.accountData.user_name;
                            // this.ePwd = this.accountData.pwd;
                            this.eMobile = this.accountData.mobile;
                            this.eNickName = this.accountData.nick_name;
                            this.id = this.accountData.city_id;
                        }
                    }
                }else{
                    this.$notify.error({
                        title: '异常',
                        offset: 100,
                        message: response.msg
                    });
                }
                })
            }else{//新增状态
              this.title = "新增分站账号信息";
              this.dialogState = false;//改变状态
              this.eName = '';//情况所有输入框数据
              this.ePwd = '';
              this.eMobile = '';
              this.eNickName ='';
              this.id = '';
            }
            this.isAddAccout = true ;
        },
        //确认弹出界面的操作
        onCommit(){ 
            let _this = this;
            //先判断是否为空
            if(this.eName==""){
                this.$notify.info({title: '提示',message: '请填写账户名称'});
                return false;
            }
            else if(this.id==""){
                this.$notify.info({title: '提示',message: '请选择所属分站'});
                return false;
            }
            else if(this.eNickName == ''){
                    this.$notify.info({title: '提示',message: '请先填写账号昵称'});
                return false;
            }
            else if(this.eMobile == '' || !(/(1[3-9]\d{9}$)/.test(this.eMobile))){
                 this.$notify.info({title: '提示',message: '请先正确填写联系电话'});
                return false;
            }
            else{
                //赋值参数
                let param ={};
                param.cityId = this.id;
                param.userName = this.eName;
                param.pwd = this.ePwd;
                param.nickName = this.eNickName;
                param.nickName = this.eNickName;
                param.mobile = this.eMobile;
                if(this.dialogState){//编辑状态
                    param.accountId = this.accountId;
                    postEditSubstationAccount(param).then(response=>{
                    console.log(response);
                    if(response.code==200){
                        this.$message.success('修改账号信息成功');
                        this.isAddAccout = false;
                        setTimeout(function(){
                        
                        _this.getDate();
                        },1500);
                    }else{
                        this.$notify.error({
                            title: '修改账号信息失败',
                            duration:2000,
                            message: '失败原因：'+response.data.msg
                        });
                    }
                    })
                }else{//新增
                    if(this.ePwd == ''){
                        this.$notify.info({title: '提示',message: '请先填写账号密码'});
                        return false;
                    }else{
                        postAddSubstationAccount(param).then(response=>{
                        console.log(response);
                        if(response.code==200){
                            this.$message.success('新增账号信息成功');
                            this.isAddAccout = false;
                            this.ePwd = '';
                            setTimeout(function(){
                                _this.getDate();
                            },1500);
                        }else{
                            this.$notify.error({
                                title: '新增账号信息失败',
                                duration:2000,
                                message: '失败原因：'+response.data.msg
                            });
                        }
                        })
                    }    
                }
            }       
        },
        //取消弹出的编辑新增弹出框
        onCancel(){
           this.isAddAccout =false;
           this.eName = '';//情况所有输入框数据
           this.ePwd = '';
            this.eMobile = '';
            this.eNickName ='';
            this.id = '';
        },
        //查询
        onSelect(){
            this.pageIndex =1;
            this.getDate();
        },
        //重置查询条件
        onResetting(){
            this.name = '';
            this.mobile = '';
            this.nickName = '';
            this.pageIndex = 1;
            this.getDate();
        },
        //分页
        handleCurrentChange(val){
            this.pageIndex =val;
            this.getDate();
        },
        //删除
        handleDelete(index,row){
            // console.log(index,row.id);
            if(row.account_id && row.account_id>0){
                this.delIndex =index;//要删除的下标
                this.delId = row.account_id; //要删除的ID
                this.delVisible = true; //显示兑换弹出框
                 //console.log(this.delVisible);
            }else{
                // console.log("没触发");
            }
        },
        // 确定删除
        deleteRow(){
            //console.log(this.delId)
            if(this.delId>0){
                console.log(this.delId)
                postDeleteSubstationAccount(this.delId).then(response=>{
                    console.log(response);
                    if(response.code==200){
                        this.$message.success('删除成功');
                        this.delVisible = false;
                        this.getDate();
                    }else{
                        this.$notify.error({
                            title: '删除失败',
                            duration:2000,
                            message: '失败原因：'+response.data.msg
                        });
                    }
                    
                })
            }
            
        }
        
    }
}
</script>
<style lang="stylus" scoped>
    .edit{
        color #ff9800;
    }
    .del{
        color #e51c23;
    }
    .download{
        color #67C23A;
    }
</style>


